package org.boulderfoodrescue.webapp.client.pickups.enterpickupreport;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.boulderfoodrescue.webapp.shared.OneTimeEvent;
import org.boulderfoodrescue.webapp.shared.Pickup;
import org.boulderfoodrescue.webapp.shared.RecurringEvent;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.datepicker.client.CalendarUtil;

public class SpecificPickupFactory {
	public static List<SpecificPickup> getSpecificPickups(Pickup pickup){
		if(pickup.getOneTimeEventPickup() != null){
			return generateOneTimePickups(pickup.getOneTimeEventPickup());
		}
		RecurringEvent re = pickup.getRecurringEventPickup();
		switch(re.getRepeatType())
		{
		case Weekly: 
			return generateWeeklyPickups(re);
		default:
			Window.alert("Not implemented for this recurring type: " + re.getRepeatType());
		}
		return null;
	}

	private static List<SpecificPickup> generateOneTimePickups(OneTimeEvent oe){
		List<SpecificPickup> sPickups = new LinkedList<SpecificPickup>();
		SpecificPickup sp = new SpecificPickup(oe.getStartTime(), 0);
		sPickups.add(sp);
		return sPickups;
	}

	@SuppressWarnings("deprecation")
	private static List<SpecificPickup> generateWeeklyPickups(RecurringEvent re){
		Date startDate = (Date) re.getStartTime().clone();
		int day = startDate.getDay();
		int index = 0;
		List<Boolean> repeatsOn = re.getRepeatsOnList();
		List<Integer> distBetweenDays = new LinkedList<Integer>();
		List<SpecificPickup> sPickups = new LinkedList<SpecificPickup>();

		int firstDay = -1;
		int lastDayFound = -1;
		Date current = null;
		for(int i=0;i<repeatsOn.size(); ++i){
			if(repeatsOn.get(i) && firstDay == -1){
				firstDay = i;
			} else if(repeatsOn.get(i) && lastDayFound == -1){
				lastDayFound = i;
				distBetweenDays.add(lastDayFound - firstDay);
			} else if(repeatsOn.get(i)){
				distBetweenDays.add(i - lastDayFound);
				lastDayFound = i;
			}
			if(current == null && i >= day && repeatsOn.get(i)){
				current = (Date) startDate.clone();
				CalendarUtil.addDaysToDate(current, i-day);
				index = distBetweenDays.size();
				System.out.println("here");
			}
		}

		if(lastDayFound == -1){
			distBetweenDays.add((int) (7*re.getFrequency()));
		} else{
			int dist = (int) (firstDay + (7-lastDayFound) + 7*(re.getFrequency()-1));
			distBetweenDays.add(dist);
		}

		if(current == null){
			current = (Date) startDate.clone();
			int dist = (int) (firstDay + (7-day) + 7*re.getFrequency());
			CalendarUtil.addDaysToDate(current, dist);
			index = distBetweenDays.size()-1;
		}
		System.out.println(index);
		for(int i : distBetweenDays)
			System.out.println(i);
		outerloop: 
			while(true){
				if(current.getTime() > (new Date().getTime())){
					break;
				}
				switch(re.getEndType()){
				case Never:
					break;
				case On:
					if(current.getTime() > re.getEndDate().getTime()){
						break outerloop;
					}
				case AfterNumOccurrences:
					if(sPickups.size() >= re.getNumberOfOccurences().intValue())
						break outerloop;
				}

				SpecificPickup sp = new SpecificPickup((Date) current.clone(), sPickups.size());
				sPickups.add(sp);

				CalendarUtil.addDaysToDate(current, distBetweenDays.get(index));
				index = (index + 1) % distBetweenDays.size();
			}
		System.out.println("size:"+sPickups.size());
		return sPickups;
	}
}
